package com.jarvis.plugin.dataredis.prefix.interceptor;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.log.Log;
import com.google.inject.Inject;
import com.jarvis.plugin.dataredis.prefix.config.DataredisPrefixProperty;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.Argument;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;

/**
 * @author xuxukang
 * @date 2019-07-05 15:44
 */
public class DataredisPrefixInterceptor {
  
  @Inject
  public static DataredisPrefixProperty dataredisPrefixProperty;
  @Inject
  public static Environment environment;

  @Advice.OnMethodEnter
  public static void enter(@Advice.Origin Method method,
    @Argument(value = 0, readOnly = false) Object key) {
    // todo
    Log.get().info("【info】{}【method】{}", "DataredisPrefixInterceptor", method.getName());
    String application = environment.getRequiredProperty("spring.application.name");

    String profile;
    String[] activeProfiles = environment.getActiveProfiles();
    if (ArrayUtil.isEmpty(activeProfiles)) {
      profile = "default";
    } else {
      profile = activeProfiles[0];
    }
  
    key = StringUtils.join(profile, ":", application, ":",
        dataredisPrefixProperty.getRedisPrefixVersion(), ":", key.toString());
    Log.get().info("【enhancedKey】{}", key);
  }

}
